﻿/*
 * Programer: Jeffrey Padfield
 * Description: Seralizes information into Binary Files.
 * Version: 1
 * Date: 01/27/2014
 */


using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace CWSaver.BinaryFiles
{
    public class Binary
    {
        /// <summary>
        /// Default Constructor.
        /// </summary>
        public Binary()
        {
            
        }

        public void BinaryWrite<T>(string fileName, T data)
        {
            try
            {
                Stream streamWrite = File.Create(fileName);
                BinaryFormatter binaryWrite = new BinaryFormatter();
                binaryWrite.Serialize(streamWrite, data);
                streamWrite.Close();
            }
            catch (Exception)
            {
                throw;
            }
            
        }

        public T BinaryRead<T>(string fileName)
        {
            try
            {
                T data;
                Stream streamRead = File.OpenRead(fileName);
                BinaryFormatter binaryRead = new BinaryFormatter();
                data = (T)binaryRead.Deserialize(streamRead);
                return data;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
